<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2018/11/23
 * Time: 14:32
 */
namespace common\services;

use common\services\tool\ToolWord;
use common\services\tool\ToolPinYin;
use Overtrue\Pinyin\Pinyin;


class CryptoService
{
    /**
     * 中文转拼音
     *
     * @param $str
     * @param string $space
     * @param string $option
     * @param string $memory
     * @return string
     */
    public static function textChangePinYin($str, $space = "", $option = PINYIN_NONE, $memory = 'small')
    {
        $strPinYin = '';
        if (empty($str)) return $strPinYin;
        switch ($memory) {
            case 'small' :   // 小内存型
                $pinyin = new Pinyin();
                break;
            case 'big' :     // 内存型
                $pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
                break;
            case 'io':       // I/O型
                $pinyin = new Pinyin('Overtrue\Pinyin\GeneratorFileDictLoader');
                break;
            default:
                $pinyin = new Pinyin();
                break;
        }
        $strArr = $pinyin->convert($str, $option);
        if (empty($space)) {
            $strPinYin = implode(" ", $strArr);
        } else {
            $strPinYin = implode($space, $strArr);
        }
        return $strPinYin;
    }

    /**
     * 中文字典
     *
     * @param $str
     * @param string $space
     * @param string $type
     * @param string $option
     * @param string $memory
     * @return string
     */
    public static function transformWithTone($str, $space = "", $type = "dictionary", $option = PINYIN_NONE, $memory = 'big')
    {
        if (empty($str)) return '';
        switch ($memory) {
            case 'small' :   // 小内存型
                $pinyin = new Pinyin();
                break;
            case 'big' :     // 内存型
                $pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
                break;
            case 'io':       // I/O型
                $pinyin = new Pinyin('Overtrue\Pinyin\GeneratorFileDictLoader');
                break;
            default:
                $pinyin = new Pinyin();
                break;
        }
        switch ($type) {
            case 'dictionary' :   // 字典
                $strArr = $pinyin->convert($str, $option);
                break;
            case 'name' :         // 人名（多音字）
                $strArr = $pinyin->name($str, $option);
                break;
            case 'addr' :         // 首字符
                return $pinyin->abbr($str, $space);
                break;
            default:
                $strArr = $pinyin->convert($str, $option);
                break;
        }
        if (empty($space)) {
            $strPinYin = implode(" ", $strArr);
        } else {
            $strPinYin = implode($space, $strArr);
        }
        return $strPinYin;
    }

    public static function testWord($str)
    {
        $auto = new Pinyin();
        // 小内存型
//        $pinyin = new Pinyin(); // 默认
        // 内存型
        $pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
        // I/O型
        // $pinyin = new Pinyin('Overtrue\Pinyin\GeneratorFileDictLoader');
        return $pinyin->convert($str, PINYIN_UNICODE);

    }


}